/*
 * @Author: your name
 * @Date: 2021-05-31 15:18:06
 * @LastEditTime: 2021-06-07 15:53:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \egg-demo\app\controller\goods.js
 */
'use strict';

const { Controller } = require('egg');

class GoodsController extends Controller {
  
  async create() {
    const { ctx } = this;
    const t_added = { created_use_uuid: '40858648-b0ec-49d4-b592-9874a5ed28e4', name: '卫龙', intro: '辣条', price: 2, goods_id: '321asd', goods_status: 'new'}
    const { createdUseUuid:created_use_uuid, name, intro, price, goods_id, goodsStatus:goods_status } = ctx.request.body;
    const getReqBody = { created_use_uuid, name, intro, price, goods_id, goods_status }
    const goods = await ctx.model.Newgoods.saveNew(getReqBody)
    ctx.helper.renderSuccess(ctx, { data: goods, message: 'ok'})
    // ctx.body = this;
  }

  async edit () {
    const { ctx } = this;
    const { uuid, name, intro, price, imgs } = ctx.request.body;
    const res = await ctx.model.Newgoods.getOne({uuid});
    console.log('res', res)
    if(!res) {
      ctx.helper.renderFail( ctx, {message: '没有查询到该商品信息', data: res })
      return
    }
    const editCondition = { uuid, name, intro, price, imgs  }
    const editData = await ctx.model.Newgoods.saveModify(editCondition)
    if(editData.length>0){
      const updatedOne = await ctx.model.Newgoods.getOne({uuid});
      ctx.helper.renderSuccess( ctx, { message: '更新成功', data: updatedOne})
    } else {
      ctx.helper.renderFail( ctx, { message: '更新失败'})
    }
  }

  async list() {
    const { ctx } = this;
    let { page, pageSize, goods_status, created_use_uuid } = ctx.request.query;
    // let filter = goods_status ? {goods_status} : {};
    let filter = {};
    if(goods_status){
      filter.goods_status = goods_status
    }
    if(created_use_uuid) {
      filter.created_use_uuid = created_use_uuid
    }
    const queryList = await ctx.model.Newgoods.queryList( {page, pageSize}, filter)
    ctx.helper.renderSuccess(ctx, { data: queryList})
  }

  async details() {
    const { ctx } = this;
    const { uuid } = ctx.request.query;
    const details = await ctx.model.Newgoods.get({uuid})
    ctx.helper.renderSuccess(ctx, { data: details, message: '请求成功'})
  }

  async editGoodsStatus() {
    const { ctx } = this;
    const { uuid, goodsStatus } = ctx.request.body;
    if(!uuid) {
      console.log('信息查询失败')
      ctx.helper.renderFail(ctx, { message: `uuid不能为空`});
      return
    }
    const editGoods = await ctx.model.Newgoods.findByPk(uuid);
    if(!editGoods) {
      console.log('信息查询失败')
      ctx.helper.renderFail(ctx, { message: `${uuid}不存在`});
      return
    }
    console.log('goodsStatus', goodsStatus)
    console.log('editGoods', editGoods)
    editGoods.goods_status = goodsStatus;
    await editGoods.save();
    ctx.helper.renderSuccess(ctx, { data: editGoods, message: 'ok'})
    // await editGoods.save({ fields: ['goods_status'] });  // 仅保存goods_status字段
  }

  async remove() {
    const { ctx } = this;
    const { uuid }  = ctx.params;
    const reqGoods = await ctx.model.Newgoods.findByPk(uuid);
    if(!reqGoods) {
      ctx.render.helper.renderFail(ctx, { message: `${uuid}不存在`})
      return
    }
    await reqGoods.destroy();
    ctx.helper.renderSuccess(ctx, { message: 'ok', data: uuid})
  }

  async tt() {
    const { ctx } = this;
    ctx.body = '这里是ceshi';
  }
}

module.exports = GoodsController;
